package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.dto.PageDto;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.mapper.DtsGrouponMapper;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGroupon;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 13:42
 */
@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {
    @Autowired
    private DtsGrouponRulesMapper dtsGrouponRulesMapper;
    @Autowired
    private DtsGoodsMapper dtsGoodsMapper;
    @Override
    public Page<DtsGrouponRules> selectPage(PageDto pageDto) {
        Page<DtsGrouponRules> dtsGrouponPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        QueryWrapper<DtsGrouponRules> dtsGrouponQueryWrapper = new QueryWrapper<>();
        // //http://localhost:8080/admin/groupon/list?page=1&limit=20&sort=add_time&order=desc
        dtsGrouponQueryWrapper.eq(pageDto.getGoodsId()!=null,"goods_id",pageDto.getGoodsId()).eq("deleted",false).orderBy(!StringUtils.isEmpty(pageDto.getSort()), "asc".equalsIgnoreCase(pageDto.getOrder()), pageDto.getSort());
        return dtsGrouponRulesMapper.selectPage(dtsGrouponPage,dtsGrouponQueryWrapper);
    }

    @Override
    public void update(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRulesMapper.updateById(dtsGrouponRules);
    }

    @Override
    public void insert(DtsGrouponRules dtsGrouponRules) {
        DtsGoods dtsGoods = dtsGoodsMapper.selectOne(new QueryWrapper<DtsGoods>().eq("id", dtsGrouponRules.getGoodsId()));
        if(dtsGoods==null){
            dtsGrouponRules.setGoodsName(UUID.randomUUID().toString());
        }
         else{dtsGrouponRules.setGoodsName(dtsGoods.getName());}

        dtsGrouponRulesMapper.insert(dtsGrouponRules);
    }

    @Override
    public void delete(DtsGrouponRules dtsGrouponRules) {
        dtsGrouponRulesMapper.updateById(dtsGrouponRules);
    }

    @Override
    public DtsGrouponRules selectById(Integer goodsId) {
        return  dtsGrouponRulesMapper.selectOne(new QueryWrapper<DtsGrouponRules>().eq("goods_id",goodsId));
    }


}
